package cn.ekuma.epos.datalogic.businesslogic.orderprocess.impl;

import com.openbravo.bean.StockDiary;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderLine;
import com.openbravo.bean.erp.OrderType;
import com.openbravo.data.basic.BasicException;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.businesslogic.orderprocess.AbstractOrderProcess;
import com.openbravo.data.loader.I_Session;

public class StoreOrderProcess extends AbstractOrderProcess {

	@Override
	public void saveOrder(I_DataLogicERP dlERP, Order order, I_Session s)
			throws BasicException {
		final OrderType oType = order.getDefineType();
		if(oType.getInOutType()==OrderType.STORE_NoChange)
			return;
		StockDiary diary;
		String moveType=oType.getName();
		boolean isCustomer=order.getCustomerId()!=null;
		boolean isCustomerStore=false;
		boolean isCustomerStore1=false;
		if(isCustomer){
			isCustomerStore=dlERP.isCustomer(order.getLocationRef());
			if (oType.getLocationRefCount() == 2&&order.getLocationRef1()!=null)
				isCustomerStore1=dlERP.isCustomer(order.getLocationRef1());
		}
		for (OrderLine l : order.getLines()) {
            if (l.getProductID() != null) {
                double multipy = l.getMultiply();
                
                if (OrderType.STORE_In == oType.getInOutType()) {
                    if (Order.ORDERBEARING_NORMAL == order.getOrderType()) {
                    } else {
                        multipy = -multipy;   
                    }
                } else if (OrderType.STORE_Out == oType.getInOutType()) {
                    if (Order.ORDERBEARING_NORMAL == order.getOrderType()) {
                        multipy = -multipy;
                    }
                } else if (OrderType.STORE_BalanceIn == oType.getInOutType()) {
                    multipy = multipy - l.getConsulMultiply();
                    
                } else if (OrderType.STORE_BalanceOut == oType.getInOutType()) {
                    multipy = l.getConsulMultiply() - multipy;
                    
                }
                if(multipy==0)
                	continue;
                // update location the stock
                diary=new StockDiary();
            	diary.setDate(order.getDate());
            	diary.setReason(moveType);
            	diary.setLocation(order.getLocationRef());
            	diary.setProductID(l.getProductID());
            	diary.setProductAttSetInstId(l.getProductAttSetInstId());
            	diary.setMultiply(multipy);
            	diary.setPrice(l.getPrice());
            	diary.setLotNo(l.getLotNo());
            	diary.setValidityDate(l.getValidityDate());
            	
            	diary.setBomStroe(l.isBomStore());
                // Hay que actualizar el stock si el hay producto
            	
                if(isCustomerStore)
                	dlERP.stockDiaryAdd(order.getCustomerId(), diary);
                else
                	dlERP.stockDiaryAdd(diary);

                
                if (oType.getLocationRefCount() == 2&&order.getLocationRef1()!=null) {
                    multipy = l.getMultiply();
                    if (OrderType.STORE_In == oType.getInOutType1()) {
                        if (Order.ORDERBEARING_NORMAL == order.getOrderType()) { 
                        } else {
                            multipy = -multipy;  
                        }
                    } else if (OrderType.STORE_Out == oType.getInOutType1()) {
                        if (Order.ORDERBEARING_NORMAL == order.getOrderType()) {
                            multipy = -multipy;
                        }
                    } else if (OrderType.STORE_BalanceIn == oType.getInOutType1()) {
                        multipy = multipy - l.getConsulMultiply();
                    } else if (OrderType.STORE_BalanceOut == oType.getInOutType1()) {
                        multipy = l.getConsulMultiply() - multipy;
                    }
                    
                    diary=new StockDiary();
                	diary.setDate(order.getDate());
                	diary.setReason(moveType);
                	diary.setLocation(order.getLocationRef1());
                	diary.setProductID(l.getProductID());
                	diary.setProductAttSetInstId(l.getProductAttSetInstId());
                	diary.setMultiply(multipy);
                	diary.setPrice(l.getPrice());
                	diary.setLotNo(l.getLotNo());
                	diary.setValidityDate(l.getValidityDate());
                	diary.setBomStroe(l.isBomStore());
                	// Hay que actualizar el stock si el hay producto
                	
                	if(isCustomerStore1)
                    	dlERP.stockDiaryAdd(order.getCustomerId(), diary);
                	else
                		dlERP.stockDiaryAdd(diary);
                }
			}
		}
	}

	@Override
	public void deleteOrder(I_DataLogicERP dlERP, Order order, I_Session s)
			throws BasicException {
		OrderType oType = order.getDefineType();
        // update the inventory
		StockDiary diary;
        String moveType = oType.getName();
        boolean isCustomer=order.getCustomerId()!=null;
		boolean isCustomerStore=false;
		boolean isCustomerStore1=false;
		if(isCustomer){
			isCustomerStore=dlERP.isCustomer(order.getLocationRef());
			if (oType.getLocationRefCount() == 2&&order.getLocationRef1()!=null)
				isCustomerStore1=dlERP.isCustomer(order.getLocationRef1());
		}
        for (int i = 0; i < order.getLinesCount(); i++) {
            OrderLine l = order.getLine(i);
            if (l.getProductID() != null && OrderType.STORE_NoChange != oType.getInOutType()) {
                double multipy = l.getMultiply();    
                if (OrderType.STORE_Out == oType.getInOutType()) {
                    if (Order.ORDERBEARING_NORMAL == order.getOrderType()) { 
                    } else {
                        multipy = -multipy; 
                    }
                } else if (OrderType.STORE_In == oType.getInOutType()) {
                    if (Order.ORDERBEARING_NORMAL == order.getOrderType()) {
                        multipy = -multipy;
                    }
                } else if (OrderType.STORE_BalanceOut == oType.getInOutType()) {
                    multipy = multipy - l.getConsulMultiply();
                    
                } else if (OrderType.STORE_BalanceIn == oType.getInOutType()) {
                    multipy = l.getConsulMultiply() - multipy; 
                }
                if(multipy==0)
                	continue;
                
                // update location the stock
                diary=new StockDiary();
            	diary.setDate(order.getDate());
            	diary.setReason(moveType);
            	diary.setLocation(order.getLocationRef());
            	diary.setProductID(l.getProductID());
            	diary.setProductAttSetInstId(l.getProductAttSetInstId());
            	diary.setMultiply(multipy);
            	diary.setPrice(l.getPrice());
            	diary.setLotNo(l.getLotNo());
            	diary.setValidityDate(l.getValidityDate());
            	diary.setBomStroe(l.isBomStore());
                // Hay que actualizar el stock si el hay producto
            	if(isCustomerStore)
            		dlERP.stockDiaryAdd(order.getCustomerId(),diary);
            	else
            	     dlERP.stockDiaryAdd(diary);
                if (oType.getLocationRefCount() == 2) {
                    multipy = l.getMultiply();
                    if (OrderType.STORE_Out == oType.getInOutType1()) {
                        if (Order.ORDERBEARING_NORMAL == order.getOrderType()) {
                        } else {
                            multipy = -multipy;
                        }
                    } else if (OrderType.STORE_In == oType.getInOutType1()) {
                        if (Order.ORDERBEARING_NORMAL == order.getOrderType()) {
                            multipy = -multipy;
                            
                        } 
                    } else if (OrderType.STORE_BalanceOut == oType.getInOutType1()) {
                        multipy = multipy - l.getConsulMultiply();
                    } else if (OrderType.STORE_BalanceIn == oType.getInOutType1()) {
                        multipy = l.getConsulMultiply() - multipy;
                    }
                    // update location the stock
                    diary=new StockDiary();
                	diary.setDate(order.getDate());
                	diary.setReason(moveType);
                	diary.setLocation(order.getLocationRef1());
                	diary.setProductID(l.getProductID());
                	diary.setProductAttSetInstId(l.getProductAttSetInstId());
                	diary.setMultiply(multipy);
                	diary.setPrice(l.getPrice());
                	diary.setLotNo(l.getLotNo());
                	diary.setValidityDate(l.getValidityDate());
                	diary.setBomStroe(l.isBomStore());
                    // Hay que actualizar el stock si el hay producto
                	if(isCustomerStore1)
                    	dlERP.stockDiaryAdd(order.getCustomerId(), diary);
                	else
                	     dlERP.stockDiaryAdd(diary);
                }
            }
        }
	}


	@Override
	public String getName() {
		return "StoreOrderProcess";
	}

}
